/**
 * ProjectName:  mall-framework
 * FileName:  SpringPropertyConfigurer.java
 * PackageName:  com.cyou.mall.springext
 * Copyright (c) 2013, CYOU All Rights Reserved.
 */
package com.blx.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * ClassName: SpringPropertyConfigurer. <br/>
 * Description: 读取spring加载的属性文件key，value <br/>
 *
 * @author Administrator 2013-3-8 下午3:25:33 <br/>
 * @version 1.0
 */
public class SpringPropertyConfigurer extends PropertyPlaceholderConfigurer {

	/**
	 * 用于存property文件的key，velue
	 */
	private final static Map<String, String> properties = new HashMap<String, String>();

	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess,
			Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		for (Object key : props.keySet()) {
			properties.put(key.toString(), props.getProperty(key.toString()));
		}
	}

	public static String getContextPropertyValue(String key) {
		return properties.get(key);
	}

	public static String getContextPropertyKey(String value) {
		Iterator<Entry<String, String>> it = properties.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = (Entry<String, String>) it.next();
			Object obj = entry.getValue();
			if (obj != null && obj.equals(value)) {
				return entry.getKey();
			}
		}
		return null;
	}
}
